#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H
#include <memory>
#include <map>
#include <thread>
#include "masters/base_controller.h"
#include "communication/asynsocketclient.h"
#include "predef.h"
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <memory>
#include <QObject>
class MainWindow;
class MainController: public QObject// public boost::enable_shared_from_this<MainController>,
{
    Q_OBJECT
public slots:
    void RecvMsgFromMasterDevice(const u_char master_index, const QVector<double> &msg);
    void OnConnectSocketCallback(const QString &addr, const unsigned short port);
    void OnSetFreqCallback(const unsigned int freq);
signals:
    void RecvMsgFromMonitor(const QString &str);
    void UpdateStatusInfo(const char attr, const QString &info);
public:
    MainController(MainWindow *main_window_);
    ~MainController();
    void Init();
    void Execute();
    void Stop();

    bool RecvFrameData(const u_char *data, const size_t len);
    void SendFrameData(const u_char *data, const size_t len);

    int32_t GetTargetRobotIndex()const  {return target_robot_index_;}
    main_controller::RunningStatusType GetRunningStatus()const  {return working_status_;}
    main_controller::SocketConnectionStatusType GetConnectionStatus()const    {return connection_status_;}

private:
    int32_t target_robot_index_;
    atomic<char> left_master_device_id_;
    atomic<char> right_master_device_id_;

    AsynSocketClientPtr socket_client_;

private:
    boost::asio::io_service io_service_;
    tcp::endpoint endpoint_;
    unsigned int control_duration_ms_;
    void TimerUpdate(const boost::system::error_code &error, boost::asio::deadline_timer *timer);

private:

    main_controller::RunningStatusType working_status_;
    main_controller::SocketConnectionStatusType connection_status_;
    bool LoadParams();
private:
    std::vector<BaseControllerPtr> master_controllers_;

    void StartRobotRC();
    void StopRobotRC();
public:
    vector<vector<double> > master_device_data_;
};
typedef boost::shared_ptr<MainController> MainControllerPtr;
#endif // MAINCONTROLLER_H
